/*
 * @Author: LaiPZ
 * @Date: 2022-08-01 10:57:36
 * @LastEditTime: 2022-08-01 17:26:40
 * @LastEditors: LaiPZ
 * @Description:
 */

const fs = require("fs");
const path = require("path");
const { resolve } = path;

const getMsg = (path = resolve(__dirname, "../article")) => {
  let res = fs.readdirSync(path);

  if (res) {
    let arr = res.map((item) => {
      if (String(item).endsWith(".md")) {
        return {
          text: item.split(".")[0],
          link: resolve(path, item),
        };
      } else {
        return {
          text: item.split(".")[0],
          items: getMsg(resolve(path, item)),
        };
      }
    });
    arr = arr.map((item) => {
      if (item.link) {
        item.link = translateDir(item.link);
      }
      return item;
    });
    return arr;
  } else {
    console.warn("无文章");
  }
};

function translateDir(path) {
  return path.replace(/\\/g, "/").split("docs")[1].split(".")[0];
}

export default {
  "/": getMsg(),
};
